﻿using DotNet.Data;
using DotNet.Utility;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XNWebModel;

namespace XNWebService
{
    public class SystemDictionaryService
    {
        private readonly Repository<SystemDictionary> sysdic = new Repository<SystemDictionary>(new AuthDatabase());
        private readonly Repository<SystemDictionaryDetails> sysdicdetail = new Repository<SystemDictionaryDetails>(new AuthDatabase());

        public List<SystemDictionaryDetails> GetDetails(string dicCode)
        {
            var db = new AuthDatabase();
            var sql = @"select b.Value,b.Name,b.SortIndex from SystemDictionary as a join SystemDictionaryDetails as b 
on a.Id = b.DictionaryId
where a.Code = @dicCode and IsEnabled=1
order by CAST(Value as int) asc";
            return db.Query<SystemDictionaryDetails>(sql, new object[] { dicCode }).ToList();
        }


        /// <summary>
        /// 根据Code值查询所有的明细项
        /// </summary>
        /// <param name="dicCode"></param>
        /// <returns></returns>
        public BoolMessage<List<SystemDictionaryDetails>> GetDetailsNoOrder(string dicCode)
        {
            var dic = GetDiction(dicCode);
            if (dic.Success)
            {
                return GetDictionDetails(dic.Data.Id);
            }
            else
            {
                return new BoolMessage<List<SystemDictionaryDetails>>(false, "未查询到数据");
            }
        }


        /// <summary>
        /// 查询code值对应的数据
        /// </summary>
        /// <param name="code"></param>
        /// <returns></returns>
        public BoolMessage<SystemDictionary> GetDiction(string code)
        {
            if (string.IsNullOrEmpty(code))
            {
                return new BoolMessage<SystemDictionary>(false, "请传入编码值");
            }

            var model = sysdic.Get(p => p.Code == code);
            if (model == null)
            {
                return new BoolMessage<SystemDictionary>(false, "无效的编码值");
            }
            return new BoolMessage<SystemDictionary>(model);
        }


        /// <summary>
        /// 通过parentid查询据有子项
        /// </summary>
        /// <param name="parentid"></param>
        /// <returns></returns>
        public BoolMessage<List<SystemDictionary>> GetSonDiction(int parentid)
        {
            var model = sysdic.Fetch(p => p.ParentId == parentid);
            if (model == null)
            {
                return new BoolMessage<List<SystemDictionary>>(false, "无效的父项Id");
            }
            return new BoolMessage<List<SystemDictionary>>(model);
        }



        /// <summary>
        /// 根据id查询所有的明细项
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public BoolMessage<List<SystemDictionaryDetails>> GetDictionDetails(int id)
        {
            var model = sysdicdetail.Fetch(p => p.DictionaryId == id && p.IsEnabled == true).OrderBy(p=>p.SortIndex).ToList();
            if (model == null)
            {
                return new BoolMessage<List<SystemDictionaryDetails>>(false, "未查询到数据");
            }
            return new BoolMessage<List<SystemDictionaryDetails>>(model);
        }


    }
}
